package cc.wsyw126.java.nowcoder.sword_offer;

/**
 * Created by Administrator on 2017/4/13.
 */
public class MoreThanHalfNum_Solution {
    public int MoreThanHalfNum_Solution(int[] array) {
        int result = 0;
        int count = 0;
        if (array.length == 0 || array == null) return result;
        for (int i = 0; i < array.length; i++) {
            if (count == 0) {
                result = array[i];
                count++;
            } else if (count > 0) {
                if (result == array[i]) count++;
                else count--;
                if (count == 0) result = 0;
            }
        }
        count =0;
        for (int i : array) {
            if (result == i) count++;
        }
        if (count<=array.length/2) result =0;
        return result;
    }

    public static void main(String[] args) {
        MoreThanHalfNum_Solution moreThanHalfNum_solution = new MoreThanHalfNum_Solution();
        int i = moreThanHalfNum_solution.MoreThanHalfNum_Solution(new int[]{2,2,2,2,2,1,3,4,5});
        System.out.println(i);
    }
}
